package com.gxa.service.zls.impl;

import com.gxa.common.R;
import com.gxa.mapper.zls.GoodsMapper;
import com.gxa.pojo.Goods;
import com.gxa.pojo.Goods_Warehouse;
import com.gxa.pojo.Warehouse_Warning;
import com.gxa.service.zls.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 周林森
 * Date: 2021/12/07 11:25:59
 * Description: 商品业务
 * Version: V1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    //删除商品
    @Override
    public R delGoods(Object goods_id) {
        int i = goodsMapper.delGoods(goods_id);
        return new R(200,"删除商品成功");
    }

    //修改商品状态
    @Override
    public R updateGoodsStatus(Object goods_id, Object status) {
        String param = "true";
        if("true".equals((String)status)){
            param = "false";
        }
        int i = goodsMapper.updateGoodsrStatus(param,goods_id);
        if (i>0){
            return new R(200,"修改状态成功");
        }
        return new R(200,"修改状态失败");
    }

    //查询商品信息
    @Override
    public R queryGoods(Map map) {
        List<Goods> goodsList = goodsMapper.queryGoodsList(map);
        return new R(200,"查询商品成功",goodsList);
    }

    //添加商品
    @Override
    public R addGoods(Map map) {
        Map goods = (Map) map.get("newgoods");
        //判断是否存在商品id
        String good = goodsMapper.queryGoodsIdExist(goods.get("goods_Id"));
        if(good != null){
            return new R(500,"添加失败");
        }
        //添加商品基本信息
        goodsMapper.addGoodsInfo(goods);
        //添加商品仓库预警
//        if(goods.getWarehouseWarningList().size()!=0){
//            for (Warehouse_Warning warning : goods.getWarehouseWarningList()) {
//                warning.setGoods_Id(goods.getGoods_Id());
//                goodsMapper.addGoodsWarn(warning);
//            }
//        }
//        //添加商品期初设置
//        if(goods.getGoodsWarehouseList().size()!=0){
//            for (Goods_Warehouse warehouse : goods.getGoodsWarehouseList()) {
//                warehouse.setGoods_Id(goods.getGoods_Id());
//                goodsMapper.addWarehouse(warehouse);
//            }
//        }
        return new R(200,"添加商品成功");
    }

    //商品库存预警查询
    @Override
    public R queryWarning(Map map) {
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        String warehouse = (String) map.get("warehouse");
        String upLow = (String) map.get("upLow");
        String input = (String) map.get("input");
        Integer page1 = 0;
        Integer limit1 = 15;
        if(page!=null && limit!=null){
            page1=(Integer.parseInt(page)-1)*Integer.parseInt(limit);
            limit1=Integer.parseInt(limit);
        }
        List<Map> list1 = goodsMapper.queryWarningCount(warehouse,input);
        List<Map> list2= goodsMapper.queryWarning(warehouse,upLow,input,page1,limit1);
        return new R(200,"成功",list2,list1);
    }

    //修改商品
    @Override
    public R updateGoodsInfo(Map map) {
        Map map1 = (Map) map.get("newgoods");
        goodsMapper.updateGoodsInfo(map1);
        return new R(200,"修改商品成功");
    }
}
